import GCJ_CLASS_TYPE = require('./../../common/GCJ_CLASS_TYPE');
import Serializer = require('./../base/Serializer');
import SJGReplaceItemBase = require('./SJGReplaceItemBase');
import SJGXY = require('./SJGXY');
import CommonModel = require('./../../common/model/CommonModel');

var CONFIG=require('../../../../common/config');
export  = SJGReplaceItemChart;

/**
 * 40折线图／41柱状图／42饼状图
 * @author 
 *
 */
class SJGReplaceItemChart extends SJGReplaceItemBase{
    //    图表名称
    public name: string="";
    //    x轴名称／y轴名称
    public xName: string="";
    public yName: string="";
    // 刻度线，当type为40折线图时有效
    public curve: SJGXY=new SJGXY(); 
    // 数据组
    public dataGroups: Array<SJGXY>=new Array<SJGXY>();
    //    数据源
    public from: string="";
    
    
    
    public constructor(g?) {
        super(g);

        this.ct = GCJ_CLASS_TYPE.CLASS_TYPE_SJGReplaceItemChart;
        this.type=SJGReplaceItemBase.TYPE_LINE_CHART;
    }
    
    // 从json对象(注意不是json字符串)中解析
    public fromJson(jsonObj,versionId) {
        super.fromJson(jsonObj,versionId);

        this.name = jsonObj.name;
        this.xName = jsonObj.xName;
        this.yName = jsonObj.yName;
        
        this.curve.fromJson(jsonObj.curve,versionId);
        
        this.dataGroups.splice(0,this.dataGroups.length);
        if (jsonObj && jsonObj.hasOwnProperty('dataGroups')) {
            for (var i = 0;i < jsonObj.dataGroups.length;i++) {
                var xy = new SJGXY();
                xy.fromJson(jsonObj.dataGroups[i],versionId);
                this.dataGroups.push(xy);
            }
        }
        
        if(jsonObj.hasOwnProperty("from")) {
            this.from=jsonObj.from;
        }
    }
    
    // 转成json字符串
    public toJson(): string {
        // 去掉父json字符串的头尾
        var superJson = super.toJson();
        superJson = superJson.substr(1,superJson.length - 2);

        var ret = '{';
        ret += superJson;
        
        // name
        ret += ',';
        ret += '"name":' + '"';
        ret += this.name + '"';
        // xName
        ret += ',';
        ret += '"xName":' + '"';
        ret += this.xName + '"';
        // yName
        ret += ',';
        ret += '"yName":' + '"';
        ret += this.yName + '"';

        // curve
        if (this.curve != null) {
            ret += ',';
            ret += '"curve":' + this.curve.toJson();
        }
        
        // dataGroups
        ret += ',';
        ret += '"dataGroups":[';
        for (var i = 0;i < this.dataGroups.length;i++) {
            if (i > 0) {
                ret += ',';
            }
            ret += this.dataGroups[i].toJson();
        }
        ret += ']';
        
        // from
        ret+=',';
        ret+='"from":'+'"';
        ret+=this.from+'"';

        ret += '}';
        return ret;
    }
    
    // 获取对象名称
    public getObjetName(): string {
        return "图表";
    }

    
    //SJG_MARK_APPEND--------------------

    public isContentEmpty():boolean {
        if(this.type == SJGReplaceItemBase.TYPE_LINE_CHART){
            if(this.dataGroups.length<1 ||this.curve.isContentEmpty()){
                return true;
            }
        }else if(this.type == SJGReplaceItemBase.TYPE_BAR_CHART){
            if( this.dataGroups.length<1 ||this.curve.isContentEmpty()){
                return true;
            }
        }else if(this.type == SJGReplaceItemBase.TYPE_PIE_CHART){
            if(this.dataGroups.length<1){
                return true;
            }
        }


        return false;
    }
}//SJG_MARK_END-------------------------
